/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// gui_controls.cpp - 

#include "ui_local.h"

#if 0

/*-----------------------------------------------------------------------------
	WINDOW :
-----------------------------------------------------------------------------*/

EGUIWindow::EGUIWindow(EGUIManager *fm, const char *name, float x, float y, float w, float h)
 : EGUIFrame(fm, name, x, y, w, h)
{
	ui->shell->AddCommand(ObjName("setText"),	(EShellCmd_f)SetText_f,		this);
	frame_opacity = 0;
	FMHideFrame();
	FMMove(x, y);
	FMResize(w, h);
}


EGUIWindow::~EGUIWindow(void)
{
	ui->shell->RemoveCommand(ObjName("setText"));
}


void EGUIWindow::SetText_f(EGUIWindow *self, int argc, char **argv) {
	self->text = argv[1];
}


bool EGUIWindow::IsVisible(void) const 
{
	if (frame_opacity>0.001) {
		return true;
	} else {
		return false;
	}
}


void EGUIWindow::FMProcess(float delta, float time)
{
	if (is_visible) {
		frame_opacity += delta / decor->GetFadeInTime();
	} else {
		frame_opacity -= delta / decor->GetFadeOutTime();
	}
	frame_opacity = Clamp<float>(frame_opacity, 0, 1);
	
	EGUIFrame::FMProcess(delta, time);
}


void EGUIWindow::FMPaint(void)
{
	uiRect_s fr = GetFrameRect();
	uiRect_s cr = GetClientRect();
	float cap_height = decor->GetWindowCaptionHeight();

	decor->DrawBorderRectTop   (fr.x, fr.y,				fr.w, cap_height,		 BORDER_SOLID, decor->GetWindowColorCaption());
	decor->DrawBorderRectBottom(fr.x, fr.y + cap_height,	fr.w, fr.h - cap_height, BORDER_SOLID, decor->GetWindowColor());
	
	decor->DrawTextInRect(fr.x, fr.y, fr.w, cap_height, decor->GetMediumFont(),  text.c_str(), EVec4(1,1,1,1));
	
	EGUIFrame::FMPaint();
}


void EGUIWindow::FMMouseClick(uint button, float x, float y)
{
	EGUIFrame::FMMouseClick(button, x, y);
}



uiRect_s EGUIWindow::GetClientRect(void) const
{
	uiRect_s client_rect;
	float cap_height = decor->GetWindowCaptionHeight();
	client_rect.x = frame_rect.x;
	client_rect.y = frame_rect.y + cap_height;
	client_rect.w = frame_rect.w;
	client_rect.h = frame_rect.h - cap_height;
	return client_rect;
}


/*-----------------------------------------------------------------------------
	BUTTON :
-----------------------------------------------------------------------------*/

EGUIButton::EGUIButton(EGUIManager *fm, const char *name, float x, float y, float w, float h)
 : EGUIFrame(fm, name, x, y, w, h)
{
	ui->shell->AddCommand(ObjName("setText"),		(EShellCmd_f)SetText_f,		this);
	ui->shell->AddCommand(ObjName("setCommand"),	(EShellCmd_f)SetCommand_f,	this);
	text	= "";
	command	= "";
	btn_down= false;
}


EGUIButton::~EGUIButton(void)
{
	ui->shell->RemoveCommand(ObjName("setText"));
	ui->shell->RemoveCommand(ObjName("setCommand"));
}


void EGUIButton::SetText_f(EGUIButton *self, int argc, char **argv) {
	self->text = argv[1];
}


void EGUIButton::SetCommand_f(EGUIButton *self, int argc, char **argv) {
	self->command = argv[1];
}


void EGUIButton::FMProcess(float delta, float time)
{
	highlight -= delta / decor->GetFadeOutTime() ;
	highlight = Clamp<float>(highlight, 0, 1);
	
	if (fm->GetFrameUnderCursor()==this) {
		highlight	=	1;
	}
}


void EGUIButton::FMPaint(void)
{
	uiRect_s fr = GetFrameRect();
	EVec4 color;

	color = decor->GetControlColorForeground();
	color = Vec4Lerp(decor->GetControlColorForeground(), decor->GetControlColorHighlight(), highlight);
	
	if (btn_down) {	
		color	=	decor->GetControlColorActivated();
	}
	
	decor->DrawBorderRect(fr.x, fr.y, fr.w, fr.h,	BORDER_SOLID, color);
	decor->DrawTextInRect(fr.x, fr.y, fr.w, fr.h,	decor->GetMediumFont(),  text.c_str(), EVec4(1,1,1,1));

	EGUIFrame::FMPaint();
}


void EGUIButton::FMMouseClick(uint button, float x, float y)
{
	EGUIFrame::FMMouseClick(button, x, y);
	shell->ExecuteString(va("%s\n", command.c_str()));
}


void EGUIButton::FMMouseIn(void)
{
}

void EGUIButton::FMMouseOut(void)
{
	btn_down = false;
}


void EGUIButton::FMMouseDown(uint button, float x, float y)
{
	btn_down = true;
}


void EGUIButton::FMMouseUp(uint button, float x, float y)
{
	btn_down = false;
}


/*-----------------------------------------------------------------------------
	Editor :	
-----------------------------------------------------------------------------*/

EEditor::EEditor(void)
{
	text	=	"";
}


EEditor::~EEditor(void)
{
}

void EEditor::InputChar(uint code)
{
	if (!isprint(code)) {
		return;
	}
	text.push_back(code);
}

void EEditor::Backspace(void)
{
	if (text.length()==0) {
		return;
	}
	text.resize(text.length()-1);
}

void EEditor::MoveCursor(int step)
{
	cursor_pos += step;
}

void EEditor::BoundCursor(void)
{
	//cursor_pos = Clamp<int>(cursor_pos, 0, text.size());
}


void EEditor::SetText(const char *text)
{
	this->text = text;
}

/*-----------------------------------------------------------------------------

-----------------------------------------------------------------------------*/

//EGUIEditBox::EGUIEditBox(EGUIManager *fm, const char *name, float x, float y, float w, float h)
// : EGUIFrame(fm, name, x, y, w, h)
//{
//	cursor_time = 0;
//	highlight = 0;
//	var = NULL;
//	shell->AddCommand(ObjName("setVar"), (EShellCmd_f)SetVar_f, this);
//}
//
//
//EGUIEditBox::~EGUIEditBox(void)
//{
//	shell->RemoveCommand(ObjName("setVar"));
//}
//
//
//void EGUIEditBox::FMKeyPress(uint code)
//{
//	editor.InputChar(code);
//}
//
//
//void EGUIEditBox::FMKeyDown(uint code)
//{
//	if (code==KEY_BACKSPACE) {
//		editor.Backspace();
//	}
//	if (code==KEY_ENTER) {
//		fm->SetActiveFrame(GetParent());
//	}
//}
//
//
//void EGUIEditBox::FMProcess(float delta, float time)
//{
//	highlight -= delta / decor->GetFadeOutTime() ;
//	highlight = Clamp<float>(highlight, 0, 1);
//	cursor_time = time;
//	
//	if (fm->GetFrameUnderCursor()==this) {
//		highlight	=	1;
//	}
//}
//
//
//void EGUIEditBox::FMCtrlReadConfig(void)
//{
//	if (var) {
//		editor.SetText(var->String());
//	} else {
//		editor.SetText("");
//	}
//}
//
//
//void EGUIEditBox::FMCtrlWriteConfig(void)
//{
//	if (var) {
//		var->SetValue( editor.GetText() );
//	}
//}
//
//
//void EGUIEditBox::FMPaint(void)
//{
//	float spacing = decor->GetTextCtrlSpacing();
//
//	EVec4	color_bg	=	decor->GetControlColorBackground();
//	EVec4	color_fg	=	decor->GetControlColorForeground();
//	
//	const char *font_name =	decor->GetSmallFont();
//
//	color_fg	=	Vec4Lerp(color_fg, decor->GetControlColorHighlight(), highlight);
//		
//	if (fm->GetActiveFrame()==this) {
//		color_fg = decor->GetControlColorActivated();
//	}
//
//	uiRect_s r  = GetFrameRect();
//	decor->DrawBorderRect(r.x, r.y, r.w, r.h, BORDER_SOLID,		color_bg);
//	decor->DrawBorderRect(r.x, r.y, r.w, r.h, BORDER_OUTLINE,	color_fg);
//	
//	uiRect_s tr = r;
//	tr.x += spacing;
//	tr.y += spacing;
//	tr.w -= (2 * spacing);
//	tr.h -= (2 * spacing);
//
//	const char *text = editor.GetText();
//	EFont *font		 = ui->RegisterFont(font_name);
//	float text_len	 = ui->GetStringLengthAdv(font, text);
//	float lh = font->GetLineHeight();
//	
//	if (fm->GetActiveFrame()==this) {
//		//	find first character :
//		while (text_len + font->GetXAdvance(' ') > r.w - 2*spacing) {
//			text++;
//			text_len = ui->GetStringLengthAdv(font, text);
//			if (text_len==0) {
//				break;
//			}
//		}
//		
//		decor->DrawTextInRect(tr.x, tr.y, tr.w, tr.h, font_name, text, EVec4(1,1,1,1), GUI_ALIGN_LEFT);
//
//		//	draw cursor :
//		float cur_pos_x = r.x + spacing + text_len;
//		float cur_pos_y = floor(r.y + (r.h + lh) / 2.0);
//		
//		quad_s q;
//		float alpha = 0.5f + 0.5f*sin(10*cursor_time);
//		q.color = EVec4(1,1,1, alpha);
//		q.uv0 = q.uv1 = EVec2(0,0);
//		q.p0  = EVec2(cur_pos_x, r.y + spacing);
//		q.p1  = EVec2(cur_pos_x + font->GetXAdvance(' '), r.y - spacing + r.h);
//		rs->DrawQuadList(1, &q, "*white");
//		
//	} else {
//		decor->DrawTextInRect(tr.x, tr.y, tr.w, tr.h, font_name, text, EVec4(1,1,1,1), GUI_ALIGN_LEFT);
//	}
//}
//
//
//void EGUIEditBox::SetVar_f(EGUIEditBox *self, int argc, char **argv)
//{
//	IConsole	*con = self->con;
//	if (argc!=2) {	
//		LOGF("usage : setVar <var name>\r\n");
//		return;
//	}
//	
//	self->var = self->shell->RegisterVar(argv[1], "", 0);
//}

#endif


















